package com.ruoyi.system.service.impl;

import java.beans.Transient;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbCardPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbPackageMapper;
import com.ruoyi.system.domain.TbPackage;
import com.ruoyi.system.service.ITbPackageService;

/**
 * 卡套餐管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-04
 */
@Service
public class TbPackageServiceImpl implements ITbPackageService 
{
    @Autowired
    private TbPackageMapper tbPackageMapper;
    @Autowired
    private TbCardPackageServiceImpl tbCardPackageService;

    /**
     * 查询卡套餐管理
     * 
     * @param id 卡套餐管理主键
     * @return 卡套餐管理
     */
    @Override
    public TbPackage selectTbPackageById(Long id)
    {
        return tbPackageMapper.selectTbPackageById(id);
    }

    /**
     * 查询卡套餐管理列表
     * 
     * @param tbPackage 卡套餐管理
     * @return 卡套餐管理
     */
    @Override
    public List<TbPackage> selectTbPackageList(TbPackage tbPackage)
    {
        return tbPackageMapper.selectTbPackageList(tbPackage);
    }

    /**
     * 新增卡套餐管理
     * 
     * @param tbPackage 卡套餐管理
     * @return 结果
     */
    @Override
    public int insertTbPackage(TbPackage tbPackage)
    {
        tbPackage.setCreateTime(DateUtils.getNowDate());
        return tbPackageMapper.insertTbPackage(tbPackage);
    }

    /**
     * 修改卡套餐管理
     * 
     * @param tbPackage 卡套餐管理
     * @return 结果
     */
    @Override
    public int updateTbPackage(TbPackage tbPackage)
    {
        tbPackage.setUpdateTime(DateUtils.getNowDate());
        return tbPackageMapper.updateTbPackage(tbPackage);
    }

    /**
     * 批量删除卡套餐管理
     * 
     * @param ids 需要删除的卡套餐管理主键
     * @return 结果
     */
    @Override
    public int deleteTbPackageByIds(Long[] ids)
    {
        return tbPackageMapper.deleteTbPackageByIds(ids);
    }

    /**
     * 删除卡套餐管理信息
     * 
     * @param id 卡套餐管理主键
     * @return 结果
     */
    @Override
    public int deleteTbPackageById(Long id)
    {
        return tbPackageMapper.deleteTbPackageById(id);
    }

    @Override
    public List<TbPackage> withCardList(TbPackage id) {
        if(id.getBangDing().equals(1)){
            return tbPackageMapper.unwithCardList(id);
        }else {
            return tbPackageMapper.withCardList(id);
        }

    }

    @Override
    @Transient
    public int setCardAndPackages(JSONObject jsonObject) {
        String packagesStr = jsonObject.getString("packageIds");
        String[] split = packagesStr.split(",");
        for (String s : split) {
            //查询卡片套餐关系有没有就添加
            TbCardPackage tbCardPackage = new TbCardPackage();
            tbCardPackage.setCardId(jsonObject.getLong("cardId"));
            tbCardPackage.setPackageId(Long.valueOf(s));
            List<TbCardPackage> tbCardPackages = tbCardPackageService.selectTbCardPackageList(tbCardPackage);
            if(tbCardPackages==null||tbCardPackages.size()<=0){
                tbCardPackage.setUpdateTime(DateUtils.getNowDate());
                tbCardPackageService.insertTbCardPackage(tbCardPackage);
            }
        }
        return 0;
    }

    @Override
    @Transient
    public int setCardAndUnPackages(JSONObject jsonObject) {
        String packagesStr = jsonObject.getString("packageIds");
        String[] split = packagesStr.split(",");
        for (String s : split) {
            //查询卡片套餐关系有没有就添加
            TbCardPackage tbCardPackage = new TbCardPackage();
            tbCardPackage.setCardId(jsonObject.getLong("cardId"));
            tbCardPackage.setPackageId(Long.valueOf(s));
            List<TbCardPackage> tbCardPackages = tbCardPackageService.selectTbCardPackageList(tbCardPackage);
            if(tbCardPackages!=null&&tbCardPackages.size()>0){
                tbCardPackageService.deleteTbCardPackageByCardAndPackage(tbCardPackage);
            }
        }
        return 0;
    }
}
